package com.android.measuringimportanceoflectureslides.activities;

import java.io.FileNotFoundException;
import java.io.IOException;

import com.android.measuringimportanceoflectureslides.R;
import com.android.measuringimportanceoflectureslides.data.PhotoHandler;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class AndroidCamera extends Activity implements SurfaceHolder.Callback {

	
	Context mContext;
	Camera camera;
	SurfaceView surfaceView;
	SurfaceHolder surfaceHolder;
	boolean previewing = false;
	LayoutInflater controlInflater = null;

	Button buttonTakePicture;

	final int RESULT_SAVEIMAGE = 0;

			
	public AndroidCamera(Context context) {
		mContext = context;
	}
	
	/** Called when the activity is first created. */
	public void startPreview() {

		((Activity) mContext).setContentView(R.layout.layout);
//		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

//		getWindow().setFormat(PixelFormat.UNKNOWN);
		surfaceView = (SurfaceView) ((Activity) mContext).findViewById(R.id.layout_xml_preview_surface_view);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

//		controlInflater = LayoutInflater.from(((ContextWrapper) mContext).getBaseContext());
//		View viewControl = controlInflater.inflate(R.layout.camera_preview_btn_overlay, null);
//		
//		LayoutParams layoutParamsControl = new LayoutParams(
//				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//		((Activity) mContext).addContentView(viewControl, layoutParamsControl);
//
//		buttonTakePicture = (Button) ((Activity) mContext).findViewById(R.id.takepicture);
//		buttonTakePicture.setOnClickListener(new Button.OnClickListener() {
//
//			public void onClick(View arg0) {
//				camera.takePicture(myShutterCallback, myPictureCallback_RAW,
//						new PhotoHandler(mContext));
//			}
//		});
//
//		LinearLayout layoutBackground = (LinearLayout) ((Activity) mContext).findViewById(R.id.background);
//		layoutBackground.setOnClickListener(new LinearLayout.OnClickListener() {
//
//			public void onClick(View arg0) {
//				buttonTakePicture.setEnabled(false);
//				camera.autoFocus(myAutoFocusCallback);
//			}
//		});
	}

	AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {

		public void onAutoFocus(boolean arg0, Camera arg1) {
			buttonTakePicture.setEnabled(true);
		}
	};

	ShutterCallback myShutterCallback = new ShutterCallback() {

		public void onShutter() {

		}
	};

	PictureCallback myPictureCallback_RAW = new PictureCallback() {

		public void onPictureTaken(byte[] arg0, Camera arg1) {

		}
	};

//	PictureCallback myPictureCallback_JPG = new PictureCallback() {
//
//		public void onPictureTaken(byte[] arg0, Camera arg1) {
//			// TODO Auto-generated method stub
//			/*
//			 * Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0,
//			 * arg0.length);
//			 */
//
//			Uri uriTarget = getContentResolver().insert(
//					Media.EXTERNAL_CONTENT_URI, new ContentValues());
//
//			OutputStream imageFileOS;
//			try {
//				imageFileOS = getContentResolver().openOutputStream(uriTarget);
//				imageFileOS.write(arg0);
//				imageFileOS.flush();
//				imageFileOS.close();
//
//				Toast.makeText(AndroidCamera.this,
//						"Image saved: " + uriTarget.toString(),
//						Toast.LENGTH_LONG).show();
//
//			} catch (FileNotFoundException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//
//			camera.startPreview();
//		}
//	};

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		if (previewing) {
			camera.stopPreview();
			previewing = false;
		}

		if (camera != null) {
			try {
				camera.setPreviewDisplay(surfaceHolder);
				camera.startPreview();
				previewing = true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void surfaceCreated(SurfaceHolder holder) {
		camera = Camera.open();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		try{
			camera.stopPreview();
			camera.release();
			camera = null;
			previewing = false;
		}
		catch (Exception e){
			Toast.makeText(mContext, "Camera Object not avaiable", Toast.LENGTH_LONG).show();
		}
	}

	public void stopPreview() {
		try{
			camera.stopPreview();
			camera.release();
			camera = null;
			previewing = false;
		}
		catch (Exception e){
			Toast.makeText(mContext, "Camera Object not avaiable", Toast.LENGTH_LONG).show();
		}
	}

	public void previewCameraOnPause() {
		// TODO Auto-generated method stub
		
	}

	public void previewCameraOnResume() {
		// TODO Auto-generated method stub
		
	}
}